﻿#ifndef __AVOID_CONTROL_CTLFONT_HEAD_FILE_INCLUDED__
#define __AVOID_CONTROL_CTLFONT_HEAD_FILE_INCLUDED__
#include "ctlDefine.h"
#include "ctlText.h"
#include "x/xctlDefine.h"
#include <memory>

namespace swui
{
	class ctlFont
	{
	public:
		enum class fontWeight : unsigned {
			dontCare = 0, thin = 100, 
			lighter = 200, light = 300, 
			normal = 400, medium = 500,
			blod = 700, bloder = 800,
			heavy = 900
		};
		enum class fontStyle : unsigned {
			none = 0,			//无
			italic = 1,			//斜体
			underline = 2,	//下划线
			strikeout = 4,	//删除线
			italicAndUnderline = italic | underline,
			italicAndStrikeout = italic | strikeout,
			underlineAndStrikeout = underline | strikeout,
			underlineAndItalic = underline | italic,
			strikeoutAndItalic = strikeout | italic,
			strikeoutAndUnderline = strikeout | underline,
			all = italic | underline | strikeout
		};
	private:
		friend std::shared_ptr<ctlFont> createFont(unsigned, ctlFont::fontStyle, ctlFont::fontWeight, const ctlText&);
		ctlFont(unsigned fontSize, fontStyle style, fontWeight weight, const ctlText& fontName);
		ctlFont(const ctlFont&);
	public:
		~ctlFont();
	public:
		bool setFontFamily(const ctlText& fontName);	//最多31个字节
		void setFontStyle(fontStyle);
		void setFontWeight(fontWeight);
		void setFontSize(unsigned fontSize);		//单位为像素
	public:
		unsigned getFontFamily(char* outData, unsigned len);
		unsigned getWideFontFamily(std::wstring::pointer outData, unsigned len);
		fontStyle getFontStyle();
		fontWeight getFontWeight();
		unsigned getFontSize();
	public:
		unsigned getMaxCharWidth();
		unsigned getAveCharWidth();
	public:
		unsigned getFontHeight();
		ctlPoint getTextSize(const char* text, int len = -1, bool calcItalic = false);
		ctlPoint getTextSize(std::wstring::const_pointer text, int len = -1, bool calcItalic = false);
		int getTextExtentPoint(std::wstring::const_pointer text, 
			unsigned textLength, unsigned width, ctlPoint* = nullptr);
		std::vector<int> getTextExtentDistance(std::wstring::const_pointer text, 
			unsigned textLength, unsigned width, ctlPoint* = nullptr);
	public:
		std::shared_ptr<ctlFont> clone();
	public:
		// 强制刷新字体
		void flush();
		//获取 HFONT 句柄
		void* getNativeHandle();
	private:
		template<typename T1, typename T2, typename T3>
		ctlPoint __getTextSize(T1, T2, T3, int, bool);
		LOGFONTW _fontData;
		int _exactHeight;
		HFONT _fontInstance;
		_ctlInterior::compatibleDC _testDC;
	};
	inline std::shared_ptr<ctlFont> createFont(unsigned fontSize = 11, ctlFont::fontStyle style = ctlFont::fontStyle::none, 
			ctlFont::fontWeight weight = ctlFont::fontWeight::normal, const ctlText& fontName = "")
	{
		return std::shared_ptr<ctlFont>(new ctlFont(fontSize, style, weight, fontName));
	}
}

#endif